﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using University.Workshop.Business;

namespace University.Workshop.WebApp
{
    public partial class Decline : System.Web.UI.Page
    {
        Offer offer = new Offer();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!User.Identity.IsAuthenticated)
                {
                    FormsAuthentication.RedirectToLoginPage();
                }
                if (Session["offer"] != null)
                {
                    offer = Session["offer"] as Offer;
                ProductName.Text = offer.RelatedProduct.Name;
                ProductPrice.Text = offer.RelatedProduct.Price.ToString();
                BuyerName.Text = UserDataAccess.GetUserById(offer.BuyerId).PersonalInformation.FirstName + " " + UserDataAccess.GetUserById(offer.BuyerId).PersonalInformation.LastName;
                OfferPrice.Text = offer.OfferPrice.ToString();
                Currency0.Text = offer.RelatedProduct.Currency;
                Currency1.Text = offer.RelatedProduct.Currency;
                }
            }
        }

        protected void YesBtn_Click(object sender, EventArgs e)
        {
            Submit.Enabled = true;
            CounterOffertbx.Enabled = true;
        }

        protected void NoBtn_Click(object sender, EventArgs e)
        {
            offer = Session["offer"] as Offer;
            offer=OfferManager.ModifyOffer(offer,"-1","Declined",true);
            Response.Redirect("UserCP.aspx");

        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            offer = Session["offer"] as Offer;

            try
            {
                offer=OfferManager.ModifyOffer(offer,CounterOffertbx.Text,"Declined",false);
                Response.Redirect("UserCP.aspx");
            }
            catch (Exception ex)
            {
                Errorlb.Text = ex.Message;
                Errorlb.Visible = true;
            }

        }
    }
}